<template>
  <div>
    <div class="search-wrap flex-end">
      <el-button type="primary" @click="addSlide">新 建</el-button>
    </div>

    <el-table border :data="tableData" style="width: 100%">
      <el-table-column align="center" type="index" label="序号" width="60"></el-table-column>
      <el-table-column align="center" prop="imgPath" label="图片链接"></el-table-column>
      <el-table-column align="center" prop="title" label="标题"></el-table-column>
      <el-table-column align="center" prop="description" label="描述"></el-table-column>
      <el-table-column align="center" prop="webPath" label="跳转链接"></el-table-column>
      <el-table-column align="center" prop="createTime" label="创建时间"></el-table-column>
      <el-table-column align="center" prop="updateTime" label="更新时间"></el-table-column>
      <el-table-column align="center" prop="isShow" label="是否展示">
        <template slot-scope="scope">
          <el-switch
                  @change="changeIsShow(scope.row)"
                  v-model="scope.row.isShow"
                  :active-value="1"
                  :inactive-value="0"
                  active-color="#13ce66"
                  inactive-color="#ff4949">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作">
        <template slot-scope="scope">
          <span class="btn text-red" @click="removeSlide(scope.row.id)">删除</span>
          <span class="btn text-blue" @click="editSlide(scope.row)">编辑</span>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog :append-to-body="true" :visible.sync="isDialog" width="600px" center>
      <slide-form v-if="isDialog" :isUpdate="isUpdate" :data="slideForm" @cancel="cancel"
                  @submitSlideForm="submitSlideForm"></slide-form>
    </el-dialog>

    <el-pagination
            class="page-wrap"
            background
            @current-change="pageChange"
            :current-page.sync="pageNum"
            :page-size="pageSize"
            layout="prev, pager, next, jumper"
            :total="total">
    </el-pagination>
  </div>
</template>

<script>
  import {queryAdminSlideList, addAndUpdateSlide, deleteSlide, updateSlideIsShow} from "../../assets/api/getData";
  import slideForm from "./slideForm";

  export default {
    name: "slideManage",
    components: {
      slideForm
    },
    data() {
      return {
        tableData: [],
        pageNum: 1,
        pageSize: 10,
        total: 0,
        isDialog: false,
        isUpdate: false,
        slideForm: {}
      }
    },
    created() {
      this.getList();
    },
    methods: {
      cancel() {
        this.isDialog = false;
      },
      async submitSlideForm(slideForm) {
        console.log(slideForm);
        if (this.isUpdate) {
          slideForm.isUpdate = true;
          slideForm.id = this.slideForm.id;
        } else {
          slideForm.isUpdate = false;
        }
        this.isDialog = false;
        let res = await addAndUpdateSlide(slideForm);
        this.getList();
      },
      pageChange() {
        this.getList();
      },
      async getList() {
        let res = await queryAdminSlideList({
          pageNum: this.pageNum,
          pageSize: this.pageSize,
        });
        this.tableData = res.data.rows;
        this.total = res.data.total;
      },
      addSlide() {
        this.isUpdate = false;
        this.slideForm = {};
        this.isDialog = true;
      },
      changeIsShow(row) {
        updateSlideIsShow({
          id: row.id,
          isShow: row.isShow
        }).then(() => {
          this.$message.success("设置成功");
          this.getList();
        }).catch(() => {
          row.isShow = row.isShow == 0 ? 1 : 0;
        })
      },
      removeSlide(id) {
        this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async () => {
          let res = await deleteSlide(id);
          this.getList();
          this.$message.success(res.message);
        }).catch(() => {
          this.$message({type: 'info', message: '已取消删除'});
        });
      },
      editSlide(row) {
        this.isUpdate = true;
        this.slideForm = row;
        this.isDialog = true;
      }
    },
  }
</script>

<style scoped>

</style>
